var UITree = function () {
    var ajaxTreeSample = function () {
        $("#tree_4").jstree({
            core: {
                themes: {
                    responsive: false
                },
                check_callback: true,
                data: {
                    url: function (node) {
                        return 'menu/load';
                    },
                    data: function (node) {
                        return {parent: node.id};
                    }
                }
            },
            types: {
                default: {
                    icon: "fa fa-folder icon-state-warning icon-lg"
                },
                file: {
                    icon: "fa fa-file icon-state-warning icon-lg"
                }
            },
            state: {
                key: "demo3"
            },
            plugins: ["contextmenu", "dnd", "state", "types"],
            contextmenu: {
                items: function () {
                    return {
                        rename: {
                            separator_before: false,
                            separator_after: false,
                            _disabled: false,
                            label: "修改名称",
                            action: function (data) {
                                var inst = $.jstree.reference(data.reference), obj = inst.get_node(data.reference);
                                inst.edit(obj);
                            }
                        },
                        icon: {
                            separator_before: false,
                            separator_after: false,
                            _disabled: false,
                            label: "设置图标",
                            action: function (data) {
                                var inst = $.jstree.reference(data.reference),
                                        obj = inst.get_node(data.reference);

                                //<button type="button" class="btn-ajax-modal" data-toggle="modal" data-modal-id="#w0" data-href="">111</button>
                                var $modal = $('seticon');
                                $modal.load('site/index', '', function () {
                                    $modal.modal();
                                });
                            }
                        },
                        remove: {
                            separator_before: false,
                            icon: false,
                            separator_after: false,
                            _disabled: false,
                            label: "删除菜单",
                            action: function (data) {
                                var inst = $.jstree.reference(data.reference), obj = inst.get_node(data.reference);
                                if (confirm('您确定要删除当前菜单？删除菜单会将全部子菜单移除。')) {
                                    $.get('menu/remove', {id: obj.id}, function (r) {
                                        if (r.status) {
                                            if (inst.is_selected(obj)) {
                                                inst.delete_node(inst.get_selected());
                                            } else {
                                                inst.delete_node(obj);
                                            }
                                        } else {
                                            alert('删除失败！');
                                        }
                                    });
                                }
                            }
                        },
                        hide: {
                            separator_before: false,
                            icon: false,
                            separator_after: false,
                            _disabled: false,
                            label: "隐藏",
                            action: function (data) {
                                var inst = $.jstree.reference(data.reference), obj = inst.get_node(data.reference);
                                $.get('menu/visible', {id: obj.id}, function (r) {
                                    if (r.visibled) {
                                        inst.enable_node(obj);
                                    } else {
                                        inst.disable_node(obj);
                                    }
                                });
                            }
                        },
                    };
                }
            },
        }).on('rename_node.jstree', function (e, data) {
            $.post('menu/rename', {"id": data.node.id, "name": data.text}, function (r) {
                if (!r.status) {
                    data.instance.set_icon(data.node, 'fa fa-warning icon-state-danger');
                }
            }).fail(function () {
                data.instance.set_icon(data.node, 'fa fa-warning icon-state-danger');
            });
            data.instance.refresh_node(data.node);
        });
    }
    return {
        //main function to initiate the module
        init: function () {
            ajaxTreeSample();
        }

    };
}();

jQuery(document).ready(function () {
    UITree.init();
});
